package com.heima.controller;

import com.heima.common.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RequestMapping("/api/vm-service/sku/fileUpload")
@RestController
public class UploadController {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint ;
    //2.配置bucket名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName ;

    /**
     * 文件上传到阿里云
     *
     */
    @PostMapping
    public String upload(MultipartFile fileName) throws Exception {
        //3.日志打印
        log.info("用户头像上传：{}",fileName.getOriginalFilename());
        //4.获取文件名
        String filename = fileName.getOriginalFilename();
        //5.获取文件后缀名
        String suffix = filename.substring(filename.lastIndexOf("."));
        //6.调用AliyunOSSUtils工具类上传文件
        String path = AliyunOSSUtils.upload(endpoint, bucketName, fileName.getBytes(), suffix);
        //7.返回响应结果
        return path;
    }

}
